/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("zm_data_record_auditing",Enums.ActionPermissionOptions.Search)]
 */
using KS3;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
using VOL.AppManager.IServices;
using VOL.Core.Enums;
using VOL.Core.Filters;
using VOL.Core.Middleware;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.ResDto;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Controllers
{
    public partial class zm_data_record_auditingController
    {
        private readonly Izm_data_record_auditingService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly KS3Client _client;
        [ActivatorUtilitiesConstructor]
        public zm_data_record_auditingController(
            Izm_data_record_auditingService service,
             KS3Client client,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service, client)
        {
            _client = client;
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [ActionLog("查询")]
        [ApiActionPermission("zm_data_record_auditing", ActionPermissionOptions.Search)]
        [HttpPost, Route("GetData")]
        public ActionResult GetData([FromBody] zmdatarecordauditingQuery param)
        {
            return JsonNormal(InvokeService("GetData", new object[] { param }));
        }

        [HttpPost, Route("GetTime")]
        public ActionResult GetTime([FromQuery] string id, string time, string t)
        {
            var res = InvokeService("GetTime", new object[] { id, time, t }) as WebResponseContent;
            return JsonNormal(res);
        }
        [HttpPost, Route("uaccepttate")]
        public ActionResult uaccepttate([FromQuery] string id, string acceptstate)
        {
            var res = InvokeService("uaccepttate", new object[] { id, acceptstate }) as WebResponseContent;
            return JsonNormal(res);
        }
        //[ApiActionPermission("zm_data_record_auditing", ActionPermissionOptions.Search)]
        [AllowAnonymous]
        [HttpGet, Route("getfile")]
        public ActionResult getfile(string key)
        {
            // 获取后缀名
            if (!string.IsNullOrEmpty(key))
            {

                var extension = Path.GetExtension(key).ToLower();
                var content_type = "";
                if (extension.Contains("pdf") || extension.Contains("PDF"))
                {
                    content_type = "application/pdf;charset=utf-8";
                }
                else
                {
                    content_type = "application/octet-stream;charset=utf-8";
                }
                Uri url = new Uri(key);
                string name = url.LocalPath;
                string filename = name.Substring(name.IndexOf('/') + 1);
                // 获取mimetype
                //var file = _client.getObject("yspt", "340000/340200/340202/安徽省-芜湖市-镜湖区-2024年城市国土空间监测-第1次预审/104c9bee-376d-47cb-aee0-c4b3e525eb8a.pdf");
                var file = _client.getObject("yspt", filename);
                // 把Content转成byte返回
                var filebyte = file.getObjectContent();
                byte[] byteArray = null;

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    filebyte.CopyTo(memoryStream);
                    // 返回内存流中的字节数组
                    byteArray = memoryStream.ToArray();
                }
                return File(byteArray, content_type);
            }
            return null;
            // 返回文件路径


        }


        /// <summary>
        /// 导出行政区、乡镇、村级工作进展数据
        /// </summary>
        /// <param name="pageRequest">type check</param>
        /// <returns></returns>
        [ActionLog("查询")]
        [ApiActionPermission("zm_data_record_auditing", ActionPermissionOptions.Export)]
        [Produces("application/octet-stream")]
        [HttpPost, Route("ExportFile")]
        //[ProducesResponseType(200)]
        //[ProducesResponseType(401)]
        public IActionResult ExportExcel([FromBody] zmdatarecordauditingQuery param)
        {
            try
            {
                var res = InvokeService("GetData", new object[] { param }) as PageGridData<zmdatarecordauditingdto>;
                var data = res.rows;
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet();
                // 第一行 标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("项目名称");
                row.CreateCell(2).SetCellValue("申请次数");
                row.CreateCell(3).SetCellValue("审核状态");
                row.CreateCell(4).SetCellValue("交验次数");
                row.CreateCell(5).SetCellValue("线下提交时间");
                row.CreateCell(6).SetCellValue("申请时间");
                row.CreateCell(7).SetCellValue("审核人");
                row.CreateCell(8).SetCellValue("审核时间");
                // 第二行 写数据
                int i = 1;
                foreach (var item in data)
                {
                    if (item != null)
                    {
                        row = sheet.CreateRow(i);
                        row.CreateCell(0).SetCellValue(i);
                        row.CreateCell(1).SetCellValue(item.projectname);
                        row.CreateCell(2).SetCellValue(item.sqcs);
                        row.CreateCell(3).SetCellValue(item.shzt);
                        row.CreateCell(4).SetCellValue(item.jycs);
                        row.CreateCell(5).SetCellValue(item.xxtj_time);
                        row.CreateCell(6).SetCellValue(item.create_time);
                        row.CreateCell(7).SetCellValue(item.sh_name);
                        row.CreateCell(8).SetCellValue(item.sh_time);
                        i++;
                    }
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "数据列表.xlsx");
            }
            catch (Exception ex)
            {
                throw;
            }

        }



        private object InvokeService(string methodName, object[] parameters)
        {
            return Service.GetType().GetMethod(methodName).Invoke(Service, parameters);
        }
    }
}
